//
//  LIUListViewController.m
//  EasyNotes
//
//  Created by 劉裕 on 24/5/2016.
//  Copyright © 2016年 劉裕. All rights reserved.
//

#import "LIUListViewController.h"
#import "LIUListCell.h"
#import "LIUEditViewController.h"
#import "LIUNote.h"
#import "LIUDetailViewController.h"
#import "NSString+Category.h"

@interface LIUListViewController ()

@property (nonatomic, strong) RLMResults<LIUNote *> *results;

@end

@implementation LIUListViewController

#pragma mark - Lazy Methods

- (RLMResults *)results {
    return [LIUNote allObjects];
}

#pragma mark - System Methods
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"title", nil);
    self.tableView.rowHeight = 80;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu"] style:UIBarButtonItemStylePlain target:self action:@selector(menuBtnClick)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(addNote)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Private Methods
- (void)menuBtnClick {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)addNote {
    LIUEditViewController *editController = [[LIUEditViewController alloc] init];
    [self.navigationController pushViewController:editController animated:YES];
}
#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.results.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LIUListCell *cell = [LIUListCell cellWithTableView:tableView reuseId:NSStringFromClass([LIUListCell class])];
    
    cell.viewModel = self.results[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WS(ws)
    UIAlertController *sheetController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"delete", nil) message:NSLocalizedString(@"isDelete", nil) preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        tableView.editing = NO;
    }];
    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"delete", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        // 删除文件
        RLMRealm *realm = [RLMRealm defaultRealm];
        LIUNote *note = ws.results[indexPath.row];
        NSString *idStr = note.idStr;
        NSString *path = [[NSString.document stringByAppendingPathComponent:@"HTML"] stringByAppendingPathComponent:idStr];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if ([fileManager fileExistsAtPath:path]) {
            [fileManager removeItemAtPath:path error:nil];
        }
        // 数据库删除
        [realm beginWriteTransaction];
        [realm deleteObject:note];
        [realm commitWriteTransaction];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }];
    [sheetController addAction:cancelAction];
    [sheetController addAction:deleteAction];
    
    [self presentViewController:sheetController animated:YES completion:nil];
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return NSLocalizedString(@"delete", nil);
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LIUDetailViewController *detail = [LIUDetailViewController new];
    detail.cell = [tableView cellForRowAtIndexPath:indexPath];
    [self.navigationController pushViewController:detail animated:YES];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

@end

/*
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    WS(ws)
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

        // 删除文件
        RLMRealm *realm = [RLMRealm defaultRealm];
        LIUNote *note = ws.results[indexPath.row];
        NSString *file = [note.idStr stringByAppendingString:@".html"];
        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:file];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if ([fileManager fileExistsAtPath:path]) {
            [fileManager removeItemAtPath:path error:nil];
        }
        // 数据库删除
        [realm beginWriteTransaction];
        [realm deleteObject:note];
        [realm commitWriteTransaction];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }];
    return @[deleteAction];
}
*/